package com.cat.dynamicPlanning;

import java.util.Arrays;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/maximum-sum-circular-subarray/
 * @create 2025/8/29 21:42
 * @since JDK17
 */

public class Solution25 {
    public int maxSubArray1(int[] nums) {
        int n = nums.length, ans = nums[0];
        int[] dp = new int[n];
        dp[0] = nums[0];
        for (int i = 1; i < n; i++) {
            dp[i] = nums[i] + Math.max(0, dp[i - 1]);
            ans = Math.max(dp[i], ans);
        }
        return ans;
    }
    public int maxSubArray2(int[] nums) {
        int n = nums.length, ans = nums[0];
        int[] dp = new int[n];
        dp[0] = nums[0];
        for (int i = 1; i < n; i++) {
            dp[i] = nums[i] + Math.min(0, dp[i - 1]);
            ans = Math.min(dp[i], ans);
        }
        return ans;
    }
    public int maxSubarraySumCircular(int[] nums) {
        int n = nums.length, sum = Arrays.stream(nums).sum();
        int ans1 = maxSubArray1(nums), ans2 = maxSubArray2(nums);
        return Math.max(ans1, sum - ans2);
    }
}
